const typeModel = require("../sequelize/type");
const shopModel = require("../sequelize/shop");
const productModel = require("../sequelize/product");

class Service {
	async list() {
		return {
			code: 200,
			message: "ok",
			result: await typeModel.findAll(),
		};
	}

	async add(body) {
		await typeModel.create(body);
		return {
			code: 200,
			message: "ok",
			result: body,
		};
	}

	async update(body) {
		await typeModel.update(body, {
			where: {
				id: body.id,
			},
		});
		return {
			code: 200,
			message: "ok",
			result: body,
		};
	}

	async del(body) {
		await typeModel.destroy({
			where: {
				id: body.id,
			},
		});
		await productModel.destroy({
			where: {
				typeId: body.id,
			},
		});
		return {
			code: 200,
			message: "ok",
			result: body,
		};
	}
}

module.exports = new Service();
